package com.suguowen.sblog.controller;

import com.suguowen.sblog.bean.User;
import com.suguowen.sblog.common.MySpringContext;
import com.suguowen.sblog.mapper.UserMapper;
import com.suguowen.sblog.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    UserService userService;

    // 跳转到login页面
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("/login")
    public String login(String name, String password, Model model, HttpSession httpSession){

        /**
         * 使用Shiro编写认证操作
         */
        // 1. 获取Subject
        Subject subject = SecurityUtils.getSubject();
        // 2. 封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(name,password);
        // 3. 执行登录方法
        try {
            subject.login(token);
            // 登录成功
            User user = userService.findByName(name);

            // 保存session已登录对象
            MySpringContext.getInstance().putUser(httpSession.getId(),user);
            return "redirect:/home";
        } catch (UnknownAccountException e) {
            // 登录失败
            model.addAttribute("msg","用户名不存在");
            return "/login";
        } catch (IncorrectCredentialsException e) {
            // 登录失败
            model.addAttribute("msg","密码错误");
            return "/login";
        }
    }

    @RequestMapping("/registration")
    public String registration(String name, String password, Model model, HttpSession httpSession){
        // 模拟数据库
        User user = userService.findByName(name);
        // 1.校验账号是否重复
        if (user == null){
            // 1. 校验账号是否合法
            if (name != null && !name.trim().equals("")){
                // 2. 校验密码是否合法
                if (password != null && !password.trim().equals("")){
                    // 注册
                    User iuser = new User();
                    iuser.setUsername(name);
                    iuser.setPassword(password);
                    userService.register(iuser);
                    // 返回登录界面
                    model.addAttribute("msg","注册成功");
                    return "forward:/toLogin";
                }else{
                    model.addAttribute("msg","密码不合法");
                    return "registration";
                }
            }else {
                model.addAttribute("msg","账号不合法");
                return "registration";
            }
        }else{
            model.addAttribute("msg","账号已存在");
            return "registration";
        }
    }

    @RequestMapping("/toRegistration")
    public String toRegistration(){
        return "registration";
    }

    @RequestMapping("/toLoginSupport")
    public String toLoginSupport(){
        return "loginSupport";
    }
    @RequestMapping("/toRegistrationSupport")
    public String toRegistrationSupport(){
        return "registrationSupport";
    }
    @RequestMapping("/toNoAuth")
    public String toNoAuth(){
        return "noAuth";
    }


}
